$("#gridDanhSachHangHoa").jqGrid({        
   	url:'/quanlygiaban/json/?func=getListGiaBan',
	datatype: "json",
   	colNames:['id', 'Mã hàng hóa', 'Hàng hóa', 'Mã khách hàng','Khách hàng', 
   	          'Giá bán', 'Giá mặc định', 'Ngày bắt đầu', 'Trạng thái'],
   	colModel:[
   		{name:'id',index:'maGiaBan', width:55, editable:false, hidden:true},
   		{name:'maHangHoa',index:'maHangHoa', width:80,align:"right", editable:true, edittype:"select", editoptions:{value: $('#lstHangHoa').val()}},
   		{name:'tenHangHoa',index:'tenHangHoa', width:90, editable:false},
   		{name:'maKhachHang',index:'maKhachHang', width:80,align:"right", editable:true, edittype:"select", editoptions:{value: $('#lstKhachHang').val()}},
   		{name:'tenKhachHang',index:'tenKhachHang', width:80, align:"right", editable:false},
   		{name:'giaBan',index:'giaBan', width:80,align:"right", editable:true},
   		{name:'giaMacDinh',index:'giaMacDinh', width:80,align:"right", editable:false},
   		{name:'ngayBatDau',index:'ngayBatDau', width:80,align:"right", editable:true,
   			editoptions: {
   		      size: 10, maxlengh: 10,
   		      dataInit: function(element) {
   		        $(element).datepicker({dateFormat: 'dd/mm/yy'})
   		      }
   			}},
   		{name:'trangThai',index:'trangThai', width:80,align:"right", editable:false},
   	],
   	rowNum:10,
   	rowList:[10,20,30],
   	pager: '#pDanhSachHangHoa',
   	sortname: 'id',
    viewrecords: true,
    loadonce: false,
    sortorder: "desc",
	jsonReader: {
		repeatitems : false,
		id: "0"
	},
	caption: "Danh sách giá bán",
	width: '971',
    height: '100%',
	editurl:"/quanlygiaban/json/?func=updateGiaBan"
});
jQuery("#gridDanhSachHangHoa").jqGrid('navGrid','#pDanhSachHangHoa',
									{
										edit:true,
										add:true,
										del:true
									});


$('#cbbTenNV').change(function() {
	//lay danh sach khach hang ve dua vao combobox
	$.get('/quanlygiaban/json/?func=getListKhachHang&maNV=' + $('#cbbTenNV').val(), function(data){
		var lstKhachHang = '';
		var select = $('#cbbKhachHang');
		select.find('option').remove();
		var obj = JSON.parse(data);
		var all = document.createElement("option");
		all.value = '';
		all.innerHTML = "Tất cả";
		select.append(all);
		for (var i=0; i < obj.length; i++)
		{
		  var option = document.createElement("option");
		  option.value = obj[i].id;
		  option.innerHTML = obj[i].tenKhachHang;
		  select.append(option);
		  
		  obj[i].tenKhachHang = obj[i].id + ' - ' + obj[i].tenKhachHang
          lstKhachHang += obj[i].id + ':' + obj[i].tenKhachHang + ';'
		}
		$('#lstKhachHang').val(lstKhachHang);
	});
});

$(document).ready(function() {
	$('#search').click(function(){
		$("#gridDanhSachHangHoa").jqGrid().setGridParam(
			{url : '/quanlygiaban/json/?func=getListGiaBan'
				+ '&maKhachHang=' + $('#cbbKhachHang').val()
				+ '&maHangHoa=' + $('#cbbHangHoa').val()}).trigger('reloadGrid');
		$("#gridDanhSachHangHoa").jqGrid().setGridParam(
				{url : '/quanlygiaban/json/?func=getListGiaBan'});
	});
	$('#HangHoa').addClass('Active');
});